<?php

namespace app\index\controller;

use app\common\controller\Base;
use think\Db;
use think\Cookie;

class Bet extends Base
{
    protected $chedan;
    protected $zhuihao;

    public function _initialize()
    {
        parent::_initialize();
        $start = [
            'user_all' => $this->user_all,
            'app_down' => $this->app_down,
            'logo' => $this->logo,
            'now_time_windows' => $this->now_time_windows
        ];
        //判断投注功能是否开启
        $zhuh_class_ifopen = Db::name('xt_tzms')->find();
        if ($zhuh_class_ifopen['chedan']) {
            $this->chedan = 1;
        } else {
            $this->chedan = 0;
        }
        //判断追号功能是否开启
        if ($zhuh_class_ifopen['zhuihao']) {
            $this->zhuihao = 1;
        } else {
            $this->zhuihao = 0;
        }
        $shortcut = input('cookie.shortcut');
        $this->assign($start);
        $this->assign('chedan', $this->chedan);
        $this->assign('shortcut', $shortcut);
        $this->assign('zhuihao', $this->zhuihao);
        $this->assign($zhuh_class_ifopen);
    }

    /**
     * @Purpose 电脑版购彩大厅
     * @Time 2017-11
     */
    public function index()
    {
        //$lottery_name = Db::name('caipai_list')->where('id',1)->find();
        $game_id = input('get.game_id', '');
        $class_id = input('get.class_id', '');
        //动态公告
        $hdlbtest = Db::name('xt_gonggao')->where('xianshi', 1)->order('paixu', 'DESC')->select();
        //彩种分类
        $openclsit = \app\index\logic\CaipaiList::getMallLotteryList();
        //跳转路径
        if ($game_id == '') {
            $dating_link = '/index/Bet/lotteryList.' . config('url_html_suffix');
        } else {
            $dating_link = '/index/Bet/lotteryBetting?game_id=' . $game_id . '&class_id=' . $class_id;
        }
        $this->assign('lblist', $hdlbtest);
        $this->assign('game_id', $game_id);
        $this->assign('dating_link', $dating_link);
        $this->assign('calss_id', $class_id);
        $this->assign($openclsit);
        return $this->fetch();
    }
    /**
     * @Purpose 电脑版购彩大厅所有彩种信息
     * @Time 2017-11
     */
    public function lotteryList()
    {
        $caipai_list = new \app\index\logic\CaipaiList();
        $openclsit = $caipai_list->getMallLotteryDetail();
        $this->assign('all_caipiao', $openclsit);
        return $this->fetch('lottery_list');
    }

    /**
     * @Purpose 电脑版彩票选注投注
     * 注:（排列五归入时时彩类，上海时时乐、排列三、福彩3D共用同一页面）
     * @Time 2017-11-22
     */
    public function lotteryBetting()
    {
        $game_id = input('game_id') ? intval(input('game_id')) : '';
        $class_id = input('class_id') ? intval(input('class_id')) : '';
        $ctime = time();

        if (!$game_id || !$class_id) {
            js_tishi('非法操作！', '/index/Bet/index');
            exit;
        }
        //判断彩种是否开启
        if ($this->lottery_if_open($game_id) != 1) {
            echo '<script>alert("抱歉，您无权访问该页面！");history.go(-1);</script>';
            exit;
        }
        //判断玩法是否开启
        if ($this->wanfa_if_open($class_id) != 1) {
            echo '<script>alert("抱歉，该玩法暂未开启！");history.go(-1);</script>';
            exit;
        }
        //查询当前彩种信息
        $cp_info = Db::name('caipai_list')->where('id', $game_id)->find();
        if (!$cp_info['cp_name_new']) $cp_info['cp_name_new'] = $cp_info['cp_name_old'];

        //查询当前彩种玩法大分类
        $play_all = Db::name('way_classify')->alias('wc')
            ->join('__LOTTERY_CLASSIFY__ lc', 'wc.lottery_class_id=lc.id')
            ->where("lc.czids REGEXP '(^|,){$game_id}($|,)'")
            ->column('wc.class_name,wc.dft_classid,lc.class_sign,lc.fandian', 'wc.id');

        //查询当前玩法相关信息
        $info = Db::name('play_classify')->alias('pc')
            ->field('pc.id class_id,pc.class_name_mini,pc.peilv_ids,wc.id')
            ->join('__WAY_CLASSIFY__ wc', 'wc.id=pc.way_class_id')
            ->where('pc.id', $class_id)
            ->find();

        //查询当前玩法涉及的赔率信息
        $wf_info = Db::name('peilv')->field('id,peilv_name,zgpl,zdpl')->where('id', 'IN', $info['peilv_ids'])->select();
        $peilvs = '';
        //组装赔率数组json
        $wf_info_json = $wf_info;
        foreach ($wf_info_json as $kwf => &$value) {
            //为时时彩组合、六合彩三中二、六合彩二中特玩法组装赔率
            if ($class_id == 2 || $class_id == 10 || $class_id == 16 || $class_id == 188 || $class_id == 204) {
                $peilvs .= $value['zgpl'] . '|';
            }
            unset($value['id']);
            unset($value['peilv_name']);
        }

        $wf_info_json = json_encode($wf_info_json);

        $wf_info['wanfa'] = $info['class_name_mini'];
        $peilvs = $peilvs ? $peilvs : $wf_info[0]['zgpl'];

        //彩种分类标识
        $class_sign = $play_all[$info['id']]['class_sign'];
        //标记当前大分类
        $play_all[$info['id']]['current'] = true;

        //开奖相关数据
        $kjjg_data_cur = Db::name('kjjg_list')->where('czid', $game_id)->where('jztime', '<', $ctime)->where('kjjg', '<>', '0')->order('jztime', 'DESC')->find();
        $kjjg_data_five = Db::name('kjjg_list')->where('czid', $game_id)->where('jztime', '<', $ctime)->where('kjjg', '<>', '0')->order('jztime', 'DESC')->limit(5)->column('qihao,kjjg');
        $kjjg_data_fur = Db::name('kjjg_list')
        					->alias('kl')
        					->join('__CAIPAI_LIST__ cl','cl.id=kl.czid')
        					->where('kl.czid', $game_id)->field('kl.*,cl.cp_fp_time')->where('kl.jztime', '>', $ctime)->where('kl.kjjg', '0')->order('kl.qihao', 'ASC')->find();
        if(!$kjjg_data_fur){
        	 $kjjg_data_fur=array('jztime'=>0,'qihao'=>'','cp_fp_time'=>0);
        }
        $kjjg_data_fur['qihao'] = $kjjg_data_fur['qihao'] ? $kjjg_data_fur['qihao'] : '加载中...';
        $kjjg_data_fur['cp_fp_time'] = $kjjg_data_fur['cp_fp_time'] ? $kjjg_data_fur['cp_fp_time'] : 0;
        $kjjg_data_fur['jztime'] = $kjjg_data_fur['jztime'] ? $kjjg_data_fur['jztime'] : time();
        $jztime_fur = $kjjg_data_fur['jztime'] - time();
        $jzt = $kjjg_data_fur['jztime'];
        $this->assign('jzt',$jzt);
        if($jztime_fur<=0){
        	$h_m_s['h']='00';
        	$h_m_s['m']='00';
        	$h_m_s['s']='00';
        }else{
        	$h_m_s['h'] = floor(($jztime_fur / 3600) % 24);
        	$h_m_s['m'] = floor(($jztime_fur / 60) % 60);
        	$h_m_s['s'] = floor($jztime_fur % 60);
        	if($h_m_s['h'] <10) $h_m_s['h'] = '0'.$h_m_s['h'];
        	if($h_m_s['m'] <10) $h_m_s['m'] = '0'.$h_m_s['m'];
        	if($h_m_s['s'] <10) $h_m_s['s'] = '0'.$h_m_s['s'];
        }
        
        
        $this->assign('h_m_s',$h_m_s);
        $this->assign('jztime_fur',$jztime_fur);
        //开奖号码拆分
        $kjjg = kjjg_split($kjjg_data_cur['kjjg'], $game_id);


        foreach ($kjjg_data_five as $k => $kdf) {
            $kjjg_data_five[$k] = kjjg_split($kdf, $game_id);
        }

        //六合彩标记颜色
        if ($game_id == 9) {
            $kjjg_sx = array();
            $kjjg_color = lhcTrans($kjjg);
            foreach ($kjjg as $key => $v) {
                if (strpos('10 22 34 46', $v) !== false) $kjjg_sx[$key] = '牛';
                if (strpos('09 21 33 45', $v) !== false) $kjjg_sx[$key] = '虎';
                if (strpos('08 20 32 44', $v) !== false) $kjjg_sx[$key] = '兔';
                if (strpos('07 19 31 43', $v) !== false) $kjjg_sx[$key] = '龙';
                if (strpos('06 18 30 42', $v) !== false) $kjjg_sx[$key] = '蛇';
                if (strpos('05 17 29 41', $v) !== false) $kjjg_sx[$key] = '马';
                if (strpos('04 16 28 40', $v) !== false) $kjjg_sx[$key] = '羊';
                if (strpos('03 15 27 39', $v) !== false) $kjjg_sx[$key] = '猴';
                if (strpos('02 14 26 38', $v) !== false) $kjjg_sx[$key] = '鸡';
                if (strpos('01 13 25 37 49', $v) !== false) $kjjg_sx[$key] = '狗';
                if (strpos('12 24 36 48', $v) !== false) $kjjg_sx[$key] = '猪';
                if (strpos('11 23 35 47', $v) !== false) $kjjg_sx[$key] = '鼠';
            }
            $this->assign('kjjg_sx', $kjjg_sx);
            $this->assign('kjjg_color', $kjjg_color);
        }
        $ext = Db::name('kjjg_list')->where('czid',$game_id)->field('qihao')->order('qihao','DESC')->find();
        $this->assign('qihao_quan',$ext['qihao']);
        
        $this->assign('game_id',$game_id);
        $this->assign('class_id',$class_id);
        $this->assign('qihao_cur',$kjjg_data_cur['qihao']);
        $this->assign('qihao_fur',$kjjg_data_fur['qihao']);
        $this->assign('kjjg',$kjjg);
        $this->assign('kjjg_five',$kjjg_data_five);
        $this->assign('play_all',$play_all);
        $this->assign('cp_info',$cp_info);
        $this->assign('wf_info',$wf_info);
        $this->assign('peilvs',$peilvs);
        $this->assign('wf_info_json',$wf_info_json);
        $this->assign('fandian',$play_all[$info['id']]['fandian']);

        $html_web = "/bet/{$class_sign}/class_{$class_id}";
        return $this->fetch($html_web);
    }


    public function shortcut()
    {
        $post = input('post.');
        $set = Cookie::set('shortcut', $post);

        if (Cookie::has('shortcut')) {
            $this->success('设置成功！');
        }
    }

}
